from pynput import mouse


# 描述
# 通过鼠标中键控制是否开启鼠标左键连点功能，中键点击开启，中键再次点击关闭。
# 点击左键，如果发现开启了连点功能，一直点击。
class MyException(Exception): pass


# 定义全局变量
persist_click_flag = False


def on_click(x, y, button, pressed):
    '''
    监听鼠标点击事件
    :param x: x坐标
    :param y: y坐标
    :param button: 按钮类型：左键、中键、右键
    :param pressed: 按压类型：点击、释放
    :return:
    '''
    global persist_click_flag
    print(f"x:{x},y:{y},button:{button},pressed:{pressed}")
    if button == mouse.Button.left:
        print(f"left mouse button pressed[{pressed}]")
    elif button == mouse.Button.middle:
        print(f"middle mouse button pressed[{pressed}] , persist_click_flag[{persist_click_flag}]")
    else:
        print(f"right mouse button pressed[{pressed}]")


# Collect events until released
# 监听鼠标点击事件
with mouse.Listener(on_click=on_click) as listener:
    try:
        persist_click_flag = False
        listener.join()
    except MyException as e:
        print('{0} was clicked'.format(e.args[0]))
